package com.yihuier.yihuier.Service.RoomManager;

import com.yihuier.yihuier.Entity.RoomManager.Room;
import com.yihuier.yihuier.Mapper.RoomManager.CompanyMapper;
import com.yihuier.yihuier.Mapper.RoomManager.RoomMapper;
import com.yihuier.yihuier.Mapper.UserManager.UserMapper;
import com.yihuier.yihuier.Service.MeetingManager.MeetingService;
import com.yihuier.yihuier.Service.UserManager.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class RoomService {

    @Autowired
    RoomMapper roomMapper;

    @Autowired
    MeetingService meetingService;

    @Autowired
    CompanyMapper companyMapper;

    /**
     * 得到会议室图片url
     * */
    public String getMeetingRoomPicUrl(int room_id){
        return roomMapper.selectMeetingRoomPicUrl(room_id);
    }

    /**
     * 得到当前广告位会议室
     * */
    public List<Room> getCarouselAdvertisementsRooms(){
        return roomMapper.selectCarouselAdvertisementsRooms();
    }

    public String getRoomNameByRoomId(Long room_id){
        if (roomMapper.selectRoomNameByRoomId(room_id) == null){
            return "会议室已注销";
        }
        return roomMapper.selectRoomNameByRoomId(room_id);
    }

    public String getRoomPicByRoomId(Number room_id){
        if (roomMapper.selectRoomPicByRoomId(room_id) == null){
            return "http://39.108.10.155:80/file/meeting_room_pic/zhuxiao.png";
        }
        return roomMapper.selectRoomPicByRoomId(room_id);
    }

    /**
     * 根据所在公司编号获得公司所有会议室状态
     * */
    public List<Room> getRoomsByCompanyId(int company_id){
        return roomMapper.selectRoomsByCompanyId(company_id);
    }

    /**
     * 根据所在公司编号为会议的时候获得公司所有会议室id和名字
     * */
    public List<Map<String,String>> getRoomsByCompanyIdForOrder(int company_id){
        return roomMapper.selectRoomsByCompanyIdForOrder(company_id);
    }

    /**
     * 模糊查询会议室
     * */
    public List<Map<String,Object>> getRoomIdAndRoomNameByVague(String content,int company_id){
        return roomMapper.selectRoomIdAndRoomNameByVague(content,company_id);
    }

    /**
     * 根据会议室编号查询会议室信息
     * */
    public Room getRoomById(int room_id){
        try{
            return roomMapper.selectRoomById(room_id);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 修改会议室名称
     */
    public int setRoomNameByRoomId(int room_id,String room_name){
        return roomMapper.setRoomNameByRoomId(room_id,room_name);
    }

    /**
     * 修改会议室容量
     */
    public int setRoomCapacityByRoomId(String room_id,String room_capacity){
        return roomMapper.setRoomCapacityByRoomId(room_id,room_capacity);
    }

    /**
     * 修改会议室状态
     */
    public int setRoomParentstatusByRoomId(String room_id,String room_parentstatus){
        return roomMapper.setRoomParentstatusByRoomId(room_id,room_parentstatus);
    }

    /**
     * 删除/取消删除会议室状态
     */
    public int setRoomStatusByRoomId(String room_id,String room_status){
        return roomMapper.setRoomStatusByRoomId(room_id,room_status);
    }

    /**
     * 添加会议室
     * */
    public int insertRoom(String room_name,String room_capacity,int room_belong_company,String room_hardware){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String create_time = df.format(new Date());
        int addResult = roomMapper.insertRoom(room_name,room_capacity,room_belong_company,room_hardware,create_time);
        if (addResult != 0){
            return  roomMapper.selectRoomIdAfterAdd(room_name,room_capacity,room_belong_company,room_hardware,create_time);
        }
        return addResult;
    }

    /**
     * 判断某个会议室是否属于某个公司
     * */
    public String judgeRoomBelongsCompany(int location,int company_id){
        return roomMapper.judgeRoomBelongsCompany(location,company_id);
    }

    /**
     * 获取所有可出租的会议室信息
     * */
    public List<Map<String,Object>> getAdvertisementRoom(){
        return roomMapper.selcetAdvertisementRoom();
    }


    public List<Room> getFitRoom(int user_id, int em_num, String[] gears){
        int companyid = this.companyMapper.selectCompanyIdOfUser(user_id);
        List<Room> rooms = this.roomMapper.selectRoomsByCompanyId(companyid);
        List<Room> result = new ArrayList<>();
        for (int i = 0 ; i < rooms.size() ;i++){
            Room temp = rooms.get(i);
            boolean thisCanQ = this.meetingService.seeRoomFit(temp, Arrays.asList(gears),em_num);
            if (thisCanQ){
                result.add(temp);
            }
        }
        return result;
    }

    /**
     * 移除所有会议室广告
     * */
    public int getAdvertisementRoomsCount(){
        return roomMapper.selcetAdvertisementRoomsCount();
    }

    /**
     * 添加会议室广告
     * */
    public int setRoomAdvertisementStatus(int room_id,int room_advertisement){
        return roomMapper.updateRoomAdvertisementStatus(room_id,room_advertisement);
    }

    public int[] getCompanyRoomIds(int company_id){
        return roomMapper.getCompanyRoomIds(company_id);
    }

}
